package com.gnerv.battle.boot.framework.core.tools.command;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Gnerv LiGen
 */
@Slf4j
public class ShellTools extends AbstractCommandTools {

    private static final String TYPE = "exec";

    private ShellTools() {
    }

    public static ExecResult exec(String command, String username, String password, String host, int port) {
        ExecResult execResult = new ExecResult();
        Session session = getSession(username, password, host, port);
        ChannelExec channelExec = null;
        try {
            Channel channel = session.openChannel(TYPE);
            channelExec = (ChannelExec) channel;
            channelExec.setCommand(command);
            channelExec.connect(getTimeOut());
        } catch (JSchException e) {
            log.info(e.getMessage());
        }
        // 3. 获取标准输入流
        BufferedReader inputStreamReader = null;
        // 4. 获取标准错误输入流
        BufferedReader errInputStreamReader = null;
        try {
            inputStreamReader = new BufferedReader(new InputStreamReader(channelExec.getInputStream()));
            errInputStreamReader = new BufferedReader(new InputStreamReader(channelExec.getErrStream()));
            execResult.setStdout(readStream(inputStreamReader));
            execResult.setStderr(readStream(errInputStreamReader));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        while (true) {
            if (channelExec.isClosed()) {
                int exitStatus = channelExec.getExitStatus();
                execResult.setStatus(exitStatus);
                break;
            }
        }
        channelExec.disconnect();
        session.disconnect();
        log.info(execResult.toString());
        return execResult;
    }

    public static ExecResult createFolders(String path, String username, String password, String host, int port) {
        String command = "mkdir -p " + path;
        return exec(command, username, password, host, port);
    }

    public static ExecResult getHomePath(String username, String password, String host, int port) {
        String command = "pwd";
        return exec(command, username, password, host, port);
    }

    public static ExecResult getOS(String username, String password, String host, int port){
        String command = "uname";
        return exec(command, username, password, host, port);
    }

    public static ExecResult getCpuFramework(String username, String password, String host, int port){
        String command = "uname -a";
        return exec(command, username, password, host, port);
    }

    public static ExecResult chmod(String username, String password, String host, int port){
        String command = "chmod";
        return exec(command, username, password, host, port);
    }



    public static void main(String[] args) throws JSchException, IOException {
        ExecResult root = getHomePath("root", "123456", "192.168.199.199", 22);
        System.out.println(root.getStatus());
        System.out.println(root.getStdout());
        System.out.println(root.getStderr());
    }

}
